package tum0r.webengine.extension

import java.io.InputStream

/**
 * 说明: InputStream扩展
 *
 * 创建者: tum0r
 *
 * 时间: 2021/8/27 13:27
 */

/**
 * 读取InputStream一行的字节数组，行分隔符为\r\n
 */
fun InputStream.readLine(): ByteArray {
    val result = ArrayList<Byte>()
    var data: Int
    val rSep = '\r'.code.toByte()
    while (this.read().also { data = it } != -1) {
        val dataByte = data.toByte()
        result.add(dataByte)
        if (dataByte == rSep) {
            result.add(this.read().toByte())
            break
        }
    }
    return result.toByteArray()
}